package org.hhh.pet.controller;

import org.hhh.basic.exception.BusinessException;
import org.hhh.basic.util.AjaxResult;
import org.hhh.basic.util.LoginContext;
import org.hhh.basic.util.PageList;
import org.hhh.pet.domain.Pet;
import org.hhh.pet.domain.SearchMasterMsg;
import org.hhh.pet.query.SearchMasterMsgQuery;
import org.hhh.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){

        return seachMasterMsgService.findAll();
    }

    /**
     * 处理寻主信息接口
     * @param pet
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet,LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 发表寻主信息接口
     * @param msg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg,HttpServletRequest request){
        try {
            //service需要返回
            seachMasterMsgService.publish(msg, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 用户个人寻主信息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        return seachMasterMsgService.user(query, LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 没有处理的寻主信息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        query.setState(0);//没有处理
        return seachMasterMsgService.pending(query, LoginContext.getCurrentLogininfo(request));
    }

    /**
     * 已经处理的寻主信息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        query.setState(1);//已经处理
        return seachMasterMsgService.processed(query, LoginContext.getCurrentLogininfo(request));
    }

}
